package cn.com.gridinfo.sooc.core.my.msg.dao;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.jeremy.arad.utils.JsonUtil;
import com.loopj.android.http.RequestParams;

import java.io.IOException;
import java.util.List;

import cn.com.gridinfo.sooc.base.http.SoocIDao;
import cn.com.gridinfo.sooc.base.http.SoocNetResult;
import cn.com.gridinfo.sooc.common.UrlConst;
import cn.com.gridinfo.sooc.core.my.msg.bean.DiscussInfo;
import cn.com.gridinfo.sooc.core.my.msg.common.CodeConst;
import cn.com.gridinfo.sooc.utils.HttpUrl;

/**
 * Created by Michelle on 2016/1/6.
 */
public class MsgDao extends SoocIDao {

    public String status="";//确认是否成功的信息，  "0": 成功  "1":服务器维护 "2"：逻辑错误
    public String statusCode="";//返回的信息
    private List<DiscussInfo> discussList;//讨论通知列表
    public MsgDao(SoocNetResult iNetResult) {
        super(iNetResult);
    }

    @Override
    public void onRequestSuccess(JsonNode result, int requestCode) throws IOException {
        if(result != null){
            if(result.has("status")) {
                status = result.get("status").asText();
            }
            if(result.has("statusCode")) {
                statusCode = result.get("statusCode").asText();
            }
            if(requestCode == CodeConst.DISCUSS_FIRST || requestCode == CodeConst.DISCUSS_PULLUP || requestCode == CodeConst.DISCUSS_PUSHDOWN){
                //讨论通知列表
                discussList = JsonUtil.node2pojo(result.get("data"), new TypeReference<List<DiscussInfo>>() {
                });
            }
        }
    }

    /**
     * 获取讨论列表
     * @param pageNo 页数
     * @param pageSize 每页的条数
     * @param code 请求码
     */
    public void getDiscuss(int pageNo,int pageSize, int code){
        String url= HttpUrl.getHttpUrl(UrlConst.Module.User.toString(), "myDiscuss", true);
        RequestParams params=new RequestParams();
        params.add("pageSize",pageSize+"");
        params.add("pageNo",pageNo+"");
        postRequest(url, params, code);
    }

    /**
     * 删除讨论通知
     * @param id 讨论通知id
     */
    public void deleteDiscuss(String id){
        String url= HttpUrl.getHttpUrl(UrlConst.Module.User.toString(), "deleteMyDiscuss", true);
        RequestParams params=new RequestParams();
        params.add("tlid",id);
        postRequest(url, params, CodeConst.DELETE_DISCUSS);
    }

    public List<DiscussInfo> getDiscussList() {
        return discussList;
    }
}
